#import <CoreFoundation/CoreFoundation.h>
/*
*
*  File:  GeneratePreviewForUrl.m
*
*  Purpose:  Create a simple QuickLook preview for XML Files.
*
*  Author:  Joel Parker (joelsparker _at_ gmail _dot_ com)
*
*
*  Credits:  (see the CREDITS file)
*
*  Copyright (C) 2008  Joel Parker (joelsparker _at_ gmail _dot_ com)
*
*  License (GNU Public License):
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/

#import <CoreServices/CoreServices.h>
#import <QuickLook/QuickLook.h>
#import <Foundation/NSObject.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSXMLDocument.h>
#import <Foundation/NSData.h>
#import <Foundation/NSError.h>
#import <Foundation/NSURL.h>
#import <Foundation/NSString.h>
#import <Foundation/NSDictionary.h>


/* -----------------------------------------------------------------------------
   Generate a preview for file

   This function's job is to create preview for designated file
   ----------------------------------------------------------------------------- */

OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options)
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	
	
	NSError *error = [[NSError alloc] init];
	NSXMLDocument *xmldoc = [[NSXMLDocument alloc] initWithContentsOfURL: (NSURL*)url options:(NSXMLNodePreserveWhitespace|NSXMLNodePreserveCDATA) error: &error];
	NSMutableDictionary *props = [[NSMutableDictionary alloc] init];
	[props setObject:@"UTF-8" forKey:(NSString *)kQLPreviewPropertyTextEncodingNameKey];
	[props setObject:@"text/xml" forKey:(NSString *)kQLPreviewPropertyMIMETypeKey];
	
	if( xmldoc == nil){
		NSLog(@"Error initializing XML Document");
	}
	else{
		NSData *data = [[NSData alloc] init];
		data = [xmldoc XMLDataWithOptions:NSXMLNodePrettyPrint];
				
		//JOEL:  The following writes out the correct data
		//[data writeToFile: @"/Users/joel/Desktop/preview.xml" atomically: YES];

	    //Using kUTTypeXML or kUTTypeHTML causes Webkit to render a blank page
		//Figure out what's up and how to use a style sheet to fix this....
		QLPreviewRequestSetDataRepresentation(preview,
			(CFDataRef)data,
			kUTTypePlainText,
			(CFDictionaryRef)props);		
	}
		
    [pool release];	
    return noErr;
}

void CancelPreviewGeneration(void* thisInterface, QLPreviewRequestRef preview)
{
    // implement only if supported
}
